<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <title>爱家</title>
    <link href="/static/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="/static/plugins/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <link href="/static/css/reset.css" rel="stylesheet">
    <link href="/static/plugins/swiper/css/swiper.min.css" rel="stylesheet">
    <link href="/static/plugins/bootstrap-datepicker/css/bootstrap-datepicker.min.css" rel="stylesheet">
    <link href="/static/css/ihome/main.css" rel="stylesheet">
    <link href="/static/css/ihome/index.css" rel="stylesheet">
</head>

<body>
    <div class="container">
        <div class="top-bar">
            <img class="logo fl" src="/static/images/logo@128x59.png">
            <div class="register-login fr">
                <a class="btn top-btn btn-theme" href="/register.html">注册</a>
                <a class="btn top-btn btn-theme" href="/login.html">登录</a>
            </div>
            <div class="user-info fr"><span><i class="fa fa-user fa-lg"></i></span> <a class="user-name" href="/my.html"></a></div>
        </div>
        <div class="swiper-container">
            <div class="swiper-wrapper">
                <script id="swiper-houses-tmpl" type="text/html">
                    {{each houses}}
                    <div class="swiper-slide">
                        <a href="/detail.html?id={{$value.house_id}}"><img src="{{$value.img_url}}"></a>
                        <div class="slide-title">{{$value.title}}</div>
                    </div>
                    {{/each}}
                </script>
            </div>
            <div class="swiper-pagination"></div>
        </div>
        <div class="search-bar">
            <button class="filter-btn" type="button" data-toggle="modal" data-target="#area-modal"><span class="fl"
                    id="area-btn">选择城区</span><span class="fr"><i
                        class="fa fa-map-marker fa-lg fa-fw"></i></span></button>
            <button class="filter-btn" type="button" data-toggle="modal" data-target="#start-date-modal"><span
                    class="fl" id="start-date-btn">入住日期</span><span class="fr"><i
                        class="fa fa-calendar fa-lg fa-fw"></i></span></button>
            <button class="filter-btn end-date" type="button" data-toggle="modal" data-target="#end-date-modal"><span
                    class="fl" id="end-date-btn">离开日期</span><span class="fr"><i
                        class="fa fa-calendar fa-lg fa-fw"></i></span></button>
            <a class="btn search-btn btn-theme" href="#" onclick="goToSearchPage(this);" area-id="" start-date=""
                end-date="">搜索</a>
            <div class="modal fade" id="area-modal" tabindex="-1" role="dialog" aria-labelledby="area-label">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                    aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="area-label">选择城区</h4>
                        </div>
                        <div class="modal-body">
                            <div class="area-list">
                                <div id="area-list-tmpl">
                                    {{each areas}}
                                    <a href="#" onclick="goToSearchPage(this)" area-id="{{$value.id}}">{{$value.name}}</a>
                                    {{/each}}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal fade" id="start-date-modal" tabindex="-1" role="dialog"
                aria-labelledby="start-date-label">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                    aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="start-date-label">入住日期</h4>
                        </div>
                        <div class="modal-body">
                            <div class="date-select" id="start-date"></div>
                            <input type="hidden" id="start-date-input">
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-theme" onclick="setStartDate();">确定</button>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal fade" id="end-date-modal" tabindex="-1" role="dialog" aria-labelledby="end-date-label">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                    aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="end-date-label">离开日期</h4>
                        </div>
                        <div class="modal-body">
                            <div class="date-select" id="end-date"></div>
                            <input type="hidden" id="end-date-input">
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-theme" onclick="setEndDate();">确定</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="footer">
            <p><span><i class="fa fa-copyright"></i></span>爱家租房&nbsp;&nbsp;享受家的温馨</p>
        </div>
    </div>

    <script src="/static/js/jquery.min.js"></script>
    <script src="/static/plugins/bootstrap/js/bootstrap.min.js"></script>
    <script src="/static/plugins/swiper/js/swiper.jquery.min.js"></script>
    <script src="/static/plugins/bootstrap-datepicker/js/bootstrap-datepicker.min.js"></script>
    <script src="/static/plugins/bootstrap-datepicker/locales/bootstrap-datepicker.zh-CN.min.js"></script>
    <script src="/static/js/template.js"></script>
    <script>
        //模态框居中的控制
            function centerModals() {
                $('.modal').each(function (i) {   //遍历每一个模态框
                    var $clone = $(this).clone().css('display', 'block').appendTo('body');
                    var top = Math.round(($clone.height() - $clone.find('.modal-content').height()) / 2);
                    top = top > 0 ? top : 0;
                    $clone.remove();
                    $(this).find('.modal-content').css("margin-top", top - 30);  //修正原先已经有的30个像素
                });
            }

            function setStartDate() {
                var startDate = $("#start-date-input").val();
                if (startDate) {
                    $(".search-btn").attr("start-date", startDate);
                    $("#start-date-btn").html(startDate);
                    $("#end-date").datepicker("destroy");
                    $("#end-date-btn").html("离开日期");
                    $("#end-date-input").val("");
                    $(".search-btn").attr("end-date", "");
                    $("#end-date").datepicker({
                        language: "zh-CN",
                        keyboardNavigation: false,
                        startDate: startDate,
                        format: "yyyy-mm-dd"
                    });
                    $("#end-date").on("changeDate", function () {
                        $("#end-date-input").val(
                            $(this).datepicker("getFormattedDate")
                        );
                    });
                    $(".end-date").show();
                }
                $("#start-date-modal").modal("hide");
            }

            function setEndDate() {
                var endDate = $("#end-date-input").val();
                if (endDate) {
                    $(".search-btn").attr("end-date", endDate);
                    $("#end-date-btn").html(endDate);
                }
                $("#end-date-modal").modal("hide");
            }

            function goToSearchPage(th) {
                var url = "/search.html?";
                url += ("aid=" + $(th).attr("area-id"));
                url += "&";
                var areaName = $(th).attr("area-name");
                if (undefined == areaName) areaName = "";
                url += ("aname=" + areaName);
                url += "&";
                url += ("sd=" + $(th).attr("start-date"));
                url += "&";
                url += ("ed=" + $(th).attr("end-date"));
                location.href = url;
            }

            $(document).ready(function () {
                // 检查用户的登录状态
                $.get("/api/v1/session", function (resp) {
                    if (0 == resp.errno) {
                        $(".top-bar .user-info .user-name").html(resp.data.name);
                        $(".top-bar>.user-info").show();
                    } else {
                        $(".top-bar>.register-login").show();
                    }
                }, "json");

                // 获取幻灯片要展示的房屋基本信息
                $.get("/api/v1/banner", function (resp) {
                    if ("0" == resp.errno) {
                        $(".swiper-wrapper").html(template("swiper-houses-tmpl", { houses: resp.data }));

                        // 设置幻灯片对象，开启幻灯片滚动
                        var mySwiper = new Swiper('.swiper-container', {
                            loop: true,
                            autoplay: 2000,
                            autoplayDisableOnInteraction: false,
                            pagination: '.swiper-pagination',
                            paginationClickable: true
                        });
                    }
                });

                // 获取城区信息
                $.get("/api/v1/areas", function (resp) {
                    if (0 == resp.errno) {
                        $(".area-list").html(template("area-list-tmpl", { areas: resp.data }));

                        $(".area-list a").click(function (e) {
                            $("#area-btn").html($(this).html());
                            $(".search-btn").attr("area-id", $(this).attr("area-id"));
                            $(".search-btn").attr("area-name", $(this).html());
                            $("#area-modal").modal("hide");
                        });
                    }
                });

                $('.modal').on('show.bs.modal', centerModals);      //当模态框出现的时候
                $(window).on('resize', centerModals);               //当窗口大小变化的时候

                $("#start-date").datepicker({
                    language: "zh-CN",
                    keyboardNavigation: false,
                    startDate: "today",
                    format: "yyyy-mm-dd"
                });

                $("#start-date").on("changeDate", function () {
                    var date = $(this).datepicker("getFormattedDate");
                    $("#start-date-input").val(date);
                });
            })
    </script>
</body>

</html>